package com.sfac.javaSpringBoot.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private ResourceProperties resourceProperties;

    //将本地文件夹注册为系统静态资源文件夹
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String systemName = System.getProperty("os.name");
        if (systemName.toLowerCase().startsWith("win")) {
            registry.addResourceHandler(resourceProperties.getPattern())
                    .addResourceLocations(ResourceUtils.FILE_URL_PREFIX + resourceProperties.getWindowLocation());
        } else {
            registry.addResourceHandler(resourceProperties.getPattern())
                    .addResourceLocations(ResourceUtils.FILE_URL_PREFIX + resourceProperties.getLinuxLocation());
        }
    }
}
